
#include "irc.h"
#include "modem.h"
#include "userio.h"
#include "gpg.h"

int main(int argc, char ** argv)
{
	int fd;
	pthread_t uiot;
	config conf;
	strcpy(conf.nick,"Default");
	char path[40];
	//On OS X modems are at tty.modem or tty.usbmodem*
	if (argc < 3)
	{
	#ifdef __APPLE__
		if (access("/dev/tty.usbmodem24680241", F_OK) != -1)
		{
			strcpy(path,"/dev/tty.usbmodem24680241");
		}
		else
		{
			strcpy(path,"/dev/tty.modem");	
		}
	//On Linux they're at ttyACM0
	#elif defined __gnu_linux__
		strcpy(path,"/dev/ttyACM0");
	#endif
	}
	else
	{
		strcpy(path,argv[1]);	
	}
//	printf("Opening %s...\n", path);
	//Setup the modem as a serial device, and open it.	
	if (setup_device(path, &fd) != 0)
		return -1;
	setup_gpg();
	//Set the file descriptor in the configuration struct.
	conf.fd = fd;
	pthread_create(&uiot,NULL,&userio, &fd);
	USLEEP(1000);
	char * blah = "AT S7=45 S0=0 L1 G1 V1 X4 &c1 E0 Q0 \r";
	send_device(fd,blah);
	
//	wait_for_signal(fd,"OK", -2);
	if (argc > 1)
	{
		connect_to(argv[argc-1],&conf);
	}	
	else
	{
		listen(&conf);
	}

	close_device(&fd);
	return EXIT_SUCCESS;
}

